package org.cicd.devops

import org.cicd.enums.BuildType
import org.cicd.modle.AppInfoVars
import org.cicd.utils.utils

/**
 * iqserver扫描
 * @return
 */
def iqServerScan(AppInfoVars appInfoVars) {
    def utils = new utils()
    utils.printMessage("包扫描", "green")
    def scanPatterns = ""

    if (appInfoVars.buildType == BuildType.MVN) {
        scanPatterns = "**/target/${appInfoVars.appName}.jar"
    } else {
        scanPatterns = "**/*.js"
    }
    catchError(buildResult: 'SUCCESS', message: '包扫描存在安全隐患') {
        dir(pwd()){
            nexusPolicyEvaluation iqApplication: manualApplication("${params.get("artifact_id")}-${params.get("app_version")}"), iqScanPatterns: [[scanPattern: "${scanPatterns}"]], iqStage: "${params.get("scan_stage")}"
        }

    }
}


/**
 * 构建包并上传至nexus
 * @param buildType
 * @return
 */
def build(AppInfoVars appInfoVars, appMap) {
    def utils = new utils()
    utils.printMessage("构建代码", "green")
    def packageCmd = appMap.packageCmd
    switch (appInfoVars.buildType) {
        case BuildType.MVN:
            utils.printMessage("构建代码 mvn 类型构建", "green")
            def shellCmd = (packageCmd==null||packageCmd=='')?"${appInfoVars.buildType} -U -B -ntp -Dmaven.test.failure.ignore=true -Dmaven.test.skip=true clean  deploy":packageCmd

            utils.exeShell(shellCmd)

            dealZipToGitRep()

            //处理script脚本
//            def shellPath = "${appInfoVars.appName}"
//            uploadScript(shellPath, params.get("artifact_id"), params.get("group_id"), params.get("app_version"))
            break
        case BuildType.NPM:
             utils.printMessage("构建代码 npm 类型构建", "green")
//            npmBuild(appInfoVars.buildType, appInfoVars.appName, params.get("group_id"), params.get("app_version"))
            //前端构建至nexus
            break
    }
}

def uploadScript(pomPath, projectArtifact, projectGroupId, projectVersion) {
    def utils = new utils()
    //判断是否存在脚本
    if ((!utils.checkFileExist("${pomPath}/script/*.sh") || !utils.checkFileExist("${pomPath}/script/*.bat") )
        && (!utils.checkFileExist("${pomPath}/build/*.sh") || !utils.checkFileExist("${pomPath}/build/*.bat"))) {
        println("不存在shell脚本")
        return
    }
    def projectDeployFile = "${projectArtifact}/deploy.properties"
    if (!utils.checkFileExist(projectDeployFile)) {
        println("不存在deploy.properties部署文件，deploy.properties 文件支持master|dev|feature/*|release/*|hotfix/*=dev,test,staging 环境部署")
        println("配置示例1:master=staging")
        println("配置示例2:feature/1.0.0=dev")
        println("配置示例3:release/1.0.0=test")
        println("配置示例4:hotfix/1.0.0=staging")
        return
    }
    //打包处理
    dealZipFile("${projectArtifact}-script-${projectVersion}.zip", path, "${projectArtifact}-script", "${projectGroupId}", "${projectVersion}")

}


/**
 * npm 打成zip包，vue不是这样打包的，私服管起来啊，没办法，屌丝码农没有话语权
 * @param env_info
 * @return
 */
def npmBuild(env_info, projectArtifact, projectGroupId, projectVersion) {
    //编译前端代码
    sh """
        npm install
        npm run ${env_info}
       """
    //打包处理
    dealZipFile("${projectArtifact}-${projectVersion}.zip", "dist/", "${projectArtifact}", "${projectGroupId}", "${projectVersion}")
}


/**
 * 处理zip打包
 * @param filePath
 * @param sourcePath
 * @param artifactId
 * @param groupId
 * @param version
 * @return
 */
def dealZipFile(filePath, sourcePath, artifactId, groupId, version) {
    def utils = new utils()
    utils.zipFile(sourcePath, filePath)
    //上传至nexus
    otherPublishToNexus(filePath, artifactId, groupId, version)
}


/**
 * 上传文件到nexus
 * @param filePath
 * @param artifactId
 * @param groupId
 * @param version
 * @return
 */
def otherPublishToNexus(filePath, artifactId, groupId, version) {
    // nexus 插件
    nexusPublisher nexusInstanceId: 'Nexus',
                   nexusRepositoryId: 'release',
                   packages: [
                           [
                            $class: 'MavenPackage',
                            mavenAssetList: [[classifier: '', extension: '', filePath: "${filePath}"]],
                            mavenCoordinate: [artifactId: "${artifactId}", groupId: "${groupId}", packaging: 'zip', version: "${version}"]
                           ]
                   ],
                   tagName: "${artifactId}-${version}"

}


def dealZipToGitRep() {
    //上传至GitRep
    def utils = new utils()
    utils.printMessage("打包上传至GitRep", "green")

}